package com.goldmantis.alitrip.common;

import com.goldmantis.alitrip.pojo.SysLog;
import com.goldmantis.alitrip.pojo.UvSysUser;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class SysLogUtil {

    public static SysLog getBaseSysLog() {

        //获取httpRequestServlet
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

        SysLog sysLog = new SysLog();
        sysLog.setOperateTime(new Date());
        sysLog.setIpAddress(request.getRemoteAddr());
        sysLog.setHost(request.getRemoteHost());
        //获取浏览器和操作系统
        sysLog.setBrowser(request.getHeader("user-agent"));
        sysLog.setxRealIp(request.getHeader("x-forwarded-for"));
        sysLog.setExecuteResult(1);
        sysLog.setExecuteResultJson(request.getRequestURL().toString());

        UvSysUser user = UserThreadLocal.get();
        if (user != null) {
            sysLog.setOperateUserId(user.userId);
            sysLog.setOperateAccount(user.getAccount().concat("(").concat(user.fullName).concat(")"));
        }

        return sysLog;


    }
}
